package coms.fourthirty.emm;

import java.util.ArrayList;

import android.app.Activity;
import android.app.LauncherActivity.ListItem;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import coms.fourthirty.emm.resources.EMSResources;
import coms.fourthirty.emm.resources.Message;

public class EMSMessages extends Activity {
	private ListView lv;
	private String username;
	private ArrayList<Message> msgs;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle grundle) {
		super.onCreate(grundle);
		setContentView(R.layout.message_list);

		Bundle extras = getIntent().getExtras();
		// username = extras.getString("coms.fourthirty.emm.username");
		username = EMSResources.username;
		msgs = EMSResources.msgs;
		// let the user know that their message was sent, if they just
		// tried to send a message
		if (extras != null && extras.getBoolean("messageSent")) {
			Toast.makeText(getBaseContext(), "Message Sent", Toast.LENGTH_SHORT)
					.show();
		}

		// if(fromNotification)
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
		mNotificationManager.cancel(EMSResources.MESSAGES_NOTIF);

		lv = (ListView) findViewById(R.id.message_list);
		lv.setAdapter(new ArrayAdapter<String>(this,
				R.layout.message_list_item, makeMessageListItemSnippets()));
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(EMSMessages.this, EMSViewMessage.class);
				// i.putExtra("coms.fourthirty.emm.username", username);
				i.putExtra("coms.fourthirty.emm.messageID", position);
				// i.putExtra("coms.fourthirty.emm.msgs", msgs);
				startActivity(i);
			}
		});
	}

	private ArrayList<String> makeMessageListItemSnippets() {
		ArrayList<String> ret = new ArrayList<String>();

		for (Message m : msgs)
			ret.add(m.from + ": " + m.subject);

		return ret;
	}

	public void createNewMessage(View view) {
		Intent i = new Intent(EMSMessages.this, EMSNewMessage.class);
		i.putExtra("coms.fourthirty.emm.username", username);
		startActivity(i);
	}
}